﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Willow.MVC.Result
{
    public static class ControllerExtension
    {
        public static JsonNetResult JsonNet(this Controller controller, object data)
        {
            return controller.JsonNet(data, JsonRequestBehavior.DenyGet, new JsonSerializerSettings { });
        }

        public static JsonNetResult JsonNet(this Controller controller, object data, JsonRequestBehavior behavior, JsonSerializerSettings settings)
        {
            return new JsonNetResult(data, behavior, settings);
        }

        /// <summary>
        /// 描述:导出Excel的Result
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static ExportExcelResult ExportExcel(this Controller controller, IEnumerable<Object> data)
        {
            DateTime time = DateTime.Now;
            var fileName = string.Format("{0}_{1}_{2}_{3}",
                time.Month, time.Day, time.Hour, time.Minute);
            return controller.ExportExcel(data, fileName);
        }


        /// <summary>
        /// 描述:导出Excel的Result
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="data">The data.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <returns></returns>
        public static ExportExcelResult ExportExcel(this Controller controller, IEnumerable<Object> data, string fileName)
        {
            return controller.ExportExcel(data, fileName, p => p);
        }

        /// <summary>
        /// 描述:导出Excel的Result
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="data">The data.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="titleFormater">The title formater.</param>
        /// <returns></returns>
        public static ExportExcelResult ExportExcel(this Controller controller, IEnumerable<Object> data, string fileName, Func<string, string> titleFormater)
        {
            ExportExcelResult result = new ExportExcelResult(data, fileName, titleFormater);
            return result;
        }

        /// <summary>
        /// 导出Word的Result
        /// </summary>
        /// <param name="templatePath">The template path.</param>
        /// <param name="placeholders">The placeholders.</param>
        /// <returns></returns>
        public static ExportWordResult ExportWord(this Controller controller, string templatePath, Dictionary<string, string> placeholders)
        {
            DateTime time = DateTime.Now;
            string fileName = string.Format("{0}_{1}_{2}_{3}", time.Month, time.Day, time.Hour, time.Minute);
            return controller.ExportWord(fileName, templatePath, placeholders);
        }

        /// <summary>
        /// 导出Word的Result
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="templatePath">The template path.</param>
        /// <param name="placeholders">The placeholders.</param>
        /// <returns></returns>
        public static ExportWordResult ExportWord(this Controller controller, string fileName, string templatePath, Dictionary<string, string> placeholders)
        {
            ExportWordResult result = new ExportWordResult(fileName, templatePath, placeholders);
            return result;
        }

        /// <summary>
        /// 标准JSON结果集
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="action">The action.</param>
        /// <returns></returns>
        public static StandardJsonResult StandardJson(this Controller controller, Action action)
        {
            return controller.StandardJson(action, "操作成功");
        }
        /// <summary>
        /// 标准JSON结果集
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="action">The action.</param>
        /// <param name="succeedMsg">The succeed MSG.</param>
        /// <returns></returns>
        public static StandardJsonResult StandardJson(this Controller controller, Action action, string succeedMsg)
        {
            StandardJsonResult result = new StandardJsonResult();
            result.Try(action, succeedMsg);
            return result;
        }
    }
}
